%%%--------------------------------------
%%% @Module  : pt_41
%%% @Author  : shebiao
%%% @Email   : shebiao@jieyou.com
%%% @Created : 2010.07.03
%%% @Description: 宠物消息的解包和组包
%%%--------------------------------------
-module(pt_41).
-export([read/2, write/2]).

%%%=========================================================================
%%% 解包函数
%%%=========================================================================

%% -----------------------------------------------------------------
%% 宠物孵化
%% -----------------------------------------------------------------
read(41001, <<GoodsId:32, GoodsUseNum:16>>) ->
    {ok, [GoodsId, GoodsUseNum]};

%% -----------------------------------------------------------------
%% 宠物放生
%% -----------------------------------------------------------------
read(41002, <<PetId:32>>) ->
    {ok, [PetId]};

%% -----------------------------------------------------------------
%% 宠物改名
%% -----------------------------------------------------------------
read(41003, <<PetId:32, Bin/binary>>) ->
    {PetName, _} = pt:read_string(Bin),
    {ok, [PetId, PetName]};

%% -----------------------------------------------------------------
%% 宠物出战
%% -----------------------------------------------------------------
read(41004, <<PetId:32, FightIconPos:16>>) ->
    {ok, [PetId, FightIconPos]};

%% -----------------------------------------------------------------
%% 宠物休息
%% -----------------------------------------------------------------
read(41005, <<PetId:32>>) ->
    {ok, [PetId]};

%% -----------------------------------------------------------------
%% 属性洗练
%% -----------------------------------------------------------------
read(41006, <<PetId:32, PayType:16, GoodsId:32, GoodsUseNum:16>>) ->
    {ok, [PetId, PayType, GoodsId, GoodsUseNum]};

%% -----------------------------------------------------------------
%% 洗练属性使用
%% -----------------------------------------------------------------
read(41007, <<PetId:32, ActionType:16>>) ->
    {ok, [PetId, ActionType]};

%% -----------------------------------------------------------------
%% 宠物开始升级
%% -----------------------------------------------------------------
read(41008, <<PetId:32>>) ->
    {ok, [PetId]};

%% -----------------------------------------------------------------
%% 宠物升级加速
%% -----------------------------------------------------------------
read(41009, <<PetId:32, ShortenTime:32>>) ->
    {ok, [PetId, ShortenTime]};

%% -----------------------------------------------------------------
%% 宠物完成升级
%% -----------------------------------------------------------------
read(41010, <<PetId:32>>) ->
    {ok, [PetId]};

%% -----------------------------------------------------------------
%% 扩展升级队列
%% -----------------------------------------------------------------
read(41011, <<>>) ->
    {ok, []};

%% -----------------------------------------------------------------
%% 宠物喂养
%% -----------------------------------------------------------------
read(41012, <<PetId:32, GoodsId:32, GoodsNum:16>>) ->
    {ok, [PetId, GoodsId, GoodsNum]};

%% -----------------------------------------------------------------
%% 宠物驯养
%% -----------------------------------------------------------------
read(41013, <<PetId:32, DomesticateType:16, GoodsId:32, GoodsNum:16>>) ->
    {ok, [PetId, DomesticateType, GoodsId, GoodsNum]};

%% -----------------------------------------------------------------
%% 宠物进阶
%% -----------------------------------------------------------------
read(41014, <<PetId:32, GoodsId:32, GoodsUseNum:16>>) ->
    {ok, [PetId, GoodsId, GoodsUseNum]};

%% -----------------------------------------------------------------
%% 体力值同步
%% -----------------------------------------------------------------
read(41015, <<>>) ->
    {ok, []};

%% -----------------------------------------------------------------
%% 获取宠物信息
%% -----------------------------------------------------------------
read(41016, <<PetId:32>>) ->
    {ok, [PetId]};

%% -----------------------------------------------------------------
%% 获取宠物列表
%% -----------------------------------------------------------------
read(41017, <<PlayerId:32>>) ->
    {ok, [PlayerId]};

%% -----------------------------------------------------------------
%% 宠物出战替换
%% -----------------------------------------------------------------
read(41018, <<PetId:32, ReplacedPetId:32>>) ->
    {ok, [PetId, ReplacedPetId]};

%% -----------------------------------------------------------------
%% 获取可用升级队列个数
%% -----------------------------------------------------------------
read(41019, <<>>) ->
    {ok, []};

%% -----------------------------------------------------------------
%% 取消升级
%% -----------------------------------------------------------------
read(41020, <<PetId:32>>) ->
    {ok, [PetId]};

%% -----------------------------------------------------------------
%% 错误处理
%% -----------------------------------------------------------------
read(_Cmd, _R) ->
    {error, no_match}.


%%%=========================================================================
%%% 组包函数
%%%=========================================================================

%% -----------------------------------------------------------------
%% 宠物通知
%% -----------------------------------------------------------------
write(41000, [MsgType]) ->
    Data = <<MsgType:16>>,
    {ok, pt:pack(41000, Data)};

%% -----------------------------------------------------------------
%% 宠物孵化
%% -----------------------------------------------------------------
write(41001, [Code, PetId, PetName]) ->
    PetNameLen = byte_size(PetName),
    Data = <<Code:16, PetId:32, PetNameLen:16, PetName/binary>>,
    {ok, pt:pack(41001, Data)};

%% -----------------------------------------------------------------
%% 宠物放生
%% -----------------------------------------------------------------
write(41002, [Code, PetId]) ->
    Data = <<Code:16, PetId:32>>,
    {ok, pt:pack(41002, Data)};

%% -----------------------------------------------------------------
%% 宠物改名
%% -----------------------------------------------------------------
write(41003, [Code, PetId, PetName, MoneyUseFlag, MoneyLeft]) ->
    PetNameLen = byte_size(PetName),
    Data = <<Code:16, PetId:32, PetNameLen:16, PetName/binary, MoneyUseFlag:16, MoneyLeft:32>>,
    {ok, pt:pack(41003, Data)};

%% -----------------------------------------------------------------
%% 宠物出战
%% -----------------------------------------------------------------
write(41004, [Code, PetId, IconPos]) ->
    Data = <<Code:16, PetId:32, IconPos:16>>,
    {ok, pt:pack(41004, Data)};

%% -----------------------------------------------------------------
%% 宠物休息
%% -----------------------------------------------------------------
write(41005, [Code, PlayerId]) ->
    Data = <<Code:16, PlayerId:32>>,
    {ok, pt:pack(41005, Data)};

%% -----------------------------------------------------------------
%% 宠物属性洗练
%% -----------------------------------------------------------------
write(41006, [Code, PetId, PayType, BaseForza, BaseWit, BaseAgile, MoneyLeft]) ->
    Data = <<Code:16, PetId:32, PayType:16, BaseForza:16, BaseWit:16, BaseAgile:16, MoneyLeft:32>>,
    {ok, pt:pack(41006, Data)};

%% -----------------------------------------------------------------
%% 洗练属性使用
%% -----------------------------------------------------------------
write(41007, [Code, PetId, ActionType, Forza, Wit, Agile]) ->
    Data = <<Code:16, PetId:32, ActionType:16, Forza:16, Wit:16, Agile:16>>,
    {ok, pt:pack(41007, Data)};

%% -----------------------------------------------------------------
%% 宠物开始升级
%% -----------------------------------------------------------------
write(41008, [Code, PetId, LeftTime, MoneyLeft]) ->
    Data = <<Code:16, PetId:32, LeftTime:32, MoneyLeft:32>>,
    {ok, pt:pack(41008, Data)};

%% -----------------------------------------------------------------
%% 宠物升级加速
%% -----------------------------------------------------------------
write(41009, [Code, PetId, ShortTime, MoneyLeft, LeftTime, Level, Forza, Wit, Agile]) ->
    Data = <<Code:16, PetId:32, ShortTime:32, MoneyLeft:32, LeftTime:32, Level:16, Forza:16, Wit:16, Agile:16>>,
    {ok, pt:pack(41009, Data)};

%% -----------------------------------------------------------------
%% 宠物完成升级
%% -----------------------------------------------------------------
write(41010, [Code, PetId, Level, Forza, Wit, Agile]) ->
    Data = <<Code:16, PetId:32, Level:16, Forza:16, Wit:16, Agile:16>>,
    {ok, pt:pack(41010, Data)};

%% -----------------------------------------------------------------
%% 扩展升级队列
%% -----------------------------------------------------------------
write(41011, [Code, QueNum, MoneyLeft]) ->
    Data = <<Code:16, QueNum:16, MoneyLeft:32>>,
    {ok, pt:pack(41011, Data)};

%% -----------------------------------------------------------------
%% 宠物喂养
%% -----------------------------------------------------------------
write(41012, [Code, PetId, Strength, Forza, Wit, Agile]) ->
    Data = <<Code:16, PetId:32, Strength:16, Forza:16, Wit:16, Agile:16>>,
    {ok, pt:pack(41012, Data)};

%% -----------------------------------------------------------------
%% 宠物驯养
%% -----------------------------------------------------------------
write(41013, [Code, PetId, DomesticateType, AptitudeAttribute, Attribute]) ->
    Data = <<Code:16, PetId:32, DomesticateType:16, AptitudeAttribute:16, Attribute:16>>,
    {ok, pt:pack(41013, Data)};

%% -----------------------------------------------------------------
%% 宠物进化
%% -----------------------------------------------------------------
write(41014, [Code, PetId, EvolutionResult, Quality, AptitudeThreshold]) ->
    Data = <<Code:16, PetId:32, EvolutionResult:16, Quality:16, AptitudeThreshold:16>>,
    {ok, pt:pack(41014, Data)};

%% -----------------------------------------------------------------
%% 体力值通知
%% -----------------------------------------------------------------
write(41015, [Code,RecordNum, Records]) ->
    Data = <<Code:16, RecordNum:16, Records/binary>>,
    {ok, pt:pack(41015, Data)};

%% -----------------------------------------------------------------
%% 获取宠物信息
%% -----------------------------------------------------------------
write(41016, [Code, Record]) ->
    Data = <<Code:16, Record/binary>>,
    {ok, pt:pack(41016, Data)};

%% -----------------------------------------------------------------
%% 获取宠物列表
%% -----------------------------------------------------------------
write(41017, [Code, RecordNum, Records]) ->
    Data = <<Code:16, RecordNum:16, Records/binary>>,
    {ok, pt:pack(41017, Data)};

%% -----------------------------------------------------------------
%% 宠物出战替换
%% -----------------------------------------------------------------
write(41018, [Code, PetId, ReplacedPetId, IconPos]) ->
    Data = <<Code:16, PetId:32, ReplacedPetId:32, IconPos:16>>,
    {ok, pt:pack(41018, Data)};

%% -----------------------------------------------------------------
%% 获取可用升级队列个数
%% -----------------------------------------------------------------
write(41019, [Code, QueNum]) ->
    Data = <<Code:16, QueNum:16>>,
    {ok, pt:pack(41019, Data)};

%% -----------------------------------------------------------------
%% 取消升级
%% -----------------------------------------------------------------
write(41020, [Code, PetId]) ->
    Data = <<Code:16, PetId:32>>,
    {ok, pt:pack(41020, Data)};

%% -----------------------------------------------------------------
%% 错误处理
%% -----------------------------------------------------------------
write(_Cmd, _R) ->
    {ok, pt:pack(0, <<>>)}.